package com.jwds.digital.bios.business.service.service.belt.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwds.digital.bios.business.core.infrastructure.dto.belt.BeltPerformanceHourReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.vo.belt.BeltPerformanceHourRespVO;
import com.jwds.digital.bios.business.service.service.belt.IBeltPerformanceDayService;
import com.jwds.digital.bios.business.core.infrastructure.dto.belt.BeltPerformanceDayReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.vo.belt.BeltPerformanceDayRespVO;
import com.jwds.digital.bios.business.core.infrastructure.dto.belt.assembler.BeltPerformanceDayAssembler;
import com.jwds.digital.bios.business.core.infrastructure.entity.belt.BeltPerformanceDay;
import com.jwds.digital.bios.business.core.infrastructure.mapper.belt.BeltPerformanceDayMapper;
import com.jwds.digital.bios.business.core.exceptions.RecordNotFoundException;
import com.jwds.tdc.framework.application.lib.page.PageRequest;
import com.jwds.tdc.framework.application.lib.page.PageResponse;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.jwds.digital.bios.business.service.service.belt.impl.BeltPerformanceHourServiceImpl.getHourRespVO;
import static com.jwds.digital.bios.business.service.service.belt.impl.BeltPerformanceHourServiceImpl.getTimeList;

/**
 * 皮带效能天表实现类
 * @author 林柏云
 * @date 2024-10-24
 */
@Slf4j
@Service
public class BeltPerformanceDayServiceImpl extends ServiceImpl<BeltPerformanceDayMapper, BeltPerformanceDay> implements IBeltPerformanceDayService {

  @Autowired
  private BeltPerformanceDayMapper baseMapper;

  @Override
  public BeltPerformanceDayRespVO queryById(Long id) {
    if(id == null) {
      return null;
    }
    BeltPerformanceDay entity = baseMapper.selectById(id);
    return BeltPerformanceDayAssembler.entity2dto(entity);
  }

  @Override
  public BeltPerformanceHourRespVO analyze(BeltPerformanceHourReqDTO reqDto) {
    // 取开始和结束时间，保留小时及以上单位时间
    //    reqDto.setStartTime(reqDto.getStartTime().withMinute(0).withSecond(0).withNano(0));
    //    reqDto.setEndTime(reqDto.getEndTime().withMinute(0).withSecond(0).withNano(0));
    reqDto.setEndTime(LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0));
    reqDto.setStartTime(reqDto.getEndTime().minusDays(1));
    BeltPerformanceHourRespVO beltPerformanceHourRespVO = baseMapper.analyze(reqDto.getId(), reqDto.getStartTime().toLocalDate().atStartOfDay(), reqDto.getEndTime().toLocalDate().atTime(23, 59, 59));
    List<LocalDateTime> timeList = getTimeList(reqDto.getStartTime().minusDays(7), reqDto.getEndTime(), ChronoUnit.DAYS);
    // 每个时间段的负载率和空载负载率
    return  getHourRespVO(reqDto, timeList, beltPerformanceHourRespVO,baseMapper,ChronoUnit.DAYS);
  }


}